<template>
  <div class="website-management">
    <el-tabs v-model="activeTab">
      <!-- 基本信息设置 -->
      <el-tab-pane label="基本信息" name="basic">
        <el-card>
          <el-form
            ref="basicFormRef"
            :model="basicForm"
            :rules="basicRules"
            label-width="120px"
          >
            <el-form-item label="网站名称" prop="siteName">
              <el-input v-model="basicForm.siteName" />
            </el-form-item>
            <el-form-item label="网站Logo" prop="logo">
              <el-upload
                class="logo-uploader"
                action="/api/upload"
                :show-file-list="false"
                :on-success="handleLogoSuccess"
              >
                <img v-if="basicForm.logo" :src="basicForm.logo" class="logo" />
                <el-icon v-else class="logo-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>
            <el-form-item label="网站描述" prop="description">
              <el-input
                v-model="basicForm.description"
                type="textarea"
                :rows="4"
              />
            </el-form-item>
            <el-form-item label="备案信息" prop="icp">
              <el-input v-model="basicForm.icp" />
            </el-form-item>
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="basicForm.phone" />
            </el-form-item>
            <el-form-item label="联系邮箱" prop="email">
              <el-input v-model="basicForm.email" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="saveBasicInfo">保存设置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-tab-pane>

      <!-- 首页轮播图 -->
      <el-tab-pane label="轮播图管理" name="banner">
        <el-card>
          <div class="banner-header">
            <el-button type="primary" @click="handleAddBanner">新增轮播图</el-button>
          </div>
          <el-table :data="bannerList" border stripe>
            <el-table-column type="index" width="50" label="序号" />
            <el-table-column label="图片预览">
              <template #default="{ row }">
                <el-image
                  :src="row.imageUrl"
                  :preview-src-list="[row.imageUrl]"
                  fit="cover"
                  class="banner-preview"
                />
              </template>
            </el-table-column>
            <el-table-column prop="title" label="标题" />
            <el-table-column prop="link" label="链接" />
            <el-table-column prop="sort" label="排序" width="80" />
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-switch
                  v-model="row.status"
                  :active-value="'active'"
                  :inactive-value="'inactive'"
                  @change="handleBannerStatusChange(row)"
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
              <template #default="{ row }">
                <el-button link type="primary" @click="handleEditBanner(row)">编辑</el-button>
                <el-button link type="danger" @click="handleDeleteBanner(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>

      <!-- 友情链接 -->
      <el-tab-pane label="友情链接" name="links">
        <el-card>
          <div class="links-header">
            <el-button type="primary" @click="handleAddLink">新增链接</el-button>
          </div>
          <el-table :data="linksList" border stripe>
            <el-table-column type="index" width="50" label="序号" />
            <el-table-column prop="name" label="名称" />
            <el-table-column prop="url" label="链接地址" />
            <el-table-column prop="logo" label="Logo">
              <template #default="{ row }">
                <el-image
                  v-if="row.logo"
                  :src="row.logo"
                  class="link-logo"
                  fit="contain"
                />
              </template>
            </el-table-column>
            <el-table-column prop="sort" label="排序" width="80" />
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-switch
                  v-model="row.status"
                  :active-value="'active'"
                  :inactive-value="'inactive'"
                  @change="handleLinkStatusChange(row)"
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
              <template #default="{ row }">
                <el-button link type="primary" @click="handleEditLink(row)">编辑</el-button>
                <el-button link type="danger" @click="handleDeleteLink(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>
    </el-tabs>

    <!-- 轮播图表单对话框 -->
    <el-dialog
      v-model="bannerDialogVisible"
      :title="bannerDialogType === 'add' ? '新增轮播图' : '编辑轮播图'"
      width="500px"
    >
      <el-form
        ref="bannerFormRef"
        :model="bannerForm"
        :rules="bannerRules"
        label-width="100px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="bannerForm.title" />
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl">
          <el-upload
            class="banner-uploader"
            action="/api/upload"
            :show-file-list="false"
            :on-success="handleBannerImageSuccess"
          >
            <img v-if="bannerForm.imageUrl" :src="bannerForm.imageUrl" class="banner" />
            <el-icon v-else class="banner-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="链接" prop="link">
          <el-input v-model="bannerForm.link" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="bannerForm.sort" :min="1" :max="99" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="bannerForm.status"
            :active-value="'active'"
            :inactive-value="'inactive'"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="bannerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveBanner">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 友情链接表单对话框 -->
    <el-dialog
      v-model="linkDialogVisible"
      :title="linkDialogType === 'add' ? '新增友情链接' : '编辑友情链接'"
      width="500px"
    >
      <el-form
        ref="linkFormRef"
        :model="linkForm"
        :rules="linkRules"
        label-width="100px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="linkForm.name" />
        </el-form-item>
        <el-form-item label="链接地址" prop="url">
          <el-input v-model="linkForm.url" />
        </el-form-item>
        <el-form-item label="Logo" prop="logo">
          <el-upload
            class="logo-uploader"
            action="/api/upload"
            :show-file-list="false"
            :on-success="handleLinkLogoSuccess"
          >
            <img v-if="linkForm.logo" :src="linkForm.logo" class="logo" />
            <el-icon v-else class="logo-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="linkForm.sort" :min="1" :max="99" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="linkForm.status"
            :active-value="'active'"
            :inactive-value="'inactive'"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="linkDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveLink">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

// 当前激活的标签页
const activeTab = ref('basic')

// 基本信息表单
const basicForm = reactive({
  siteName: '海洋生态志愿服务平台',
  logo: '',
  description: '',
  icp: '',
  phone: '',
  email: ''
})

// 基本信息表单校验规则
const basicRules = {
  siteName: [
    { required: true, message: '请输入网站名称', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入网站描述', trigger: 'blur' }
  ]
}

// 轮播图列表
const bannerList = ref([
  {
    id: 1,
    imageUrl: 'https://example.com/banner1.jpg',
    title: '志愿者招募',
    link: '/volunteer',
    sort: 1,
    status: 'active'
  }
])

// 友情链接列表
const linksList = ref([
  {
    id: 1,
    name: '海洋保护协会',
    url: 'https://example.com',
    logo: 'https://example.com/logo.png',
    sort: 1,
    status: 'active'
  }
])

// 对话框控制
const bannerDialogVisible = ref(false)
const bannerDialogType = ref('add')
const linkDialogVisible = ref(false)
const linkDialogType = ref('add')

// 表单数据
const bannerForm = reactive({
  title: '',
  imageUrl: '',
  link: '',
  sort: 1,
  status: 'active'
})

const linkForm = reactive({
  name: '',
  url: '',
  logo: '',
  sort: 1,
  status: 'active'
})

// 保存基本信息
const saveBasicInfo = () => {
  ElMessage.success('保存成功')
}

// 处理Logo上传成功
const handleLogoSuccess = (res) => {
  basicForm.logo = res.url
}

// 轮播图相关方法
const handleAddBanner = () => {
  bannerDialogType.value = 'add'
  bannerDialogVisible.value = true
  Object.keys(bannerForm).forEach(key => {
    bannerForm[key] = key === 'sort' ? 1 : key === 'status' ? 'active' : ''
  })
}

const handleEditBanner = (row) => {
  bannerDialogType.value = 'edit'
  bannerDialogVisible.value = true
  Object.keys(bannerForm).forEach(key => {
    bannerForm[key] = row[key]
  })
}

const handleDeleteBanner = (row) => {
  ElMessage.success('删除成功')
}

const handleBannerStatusChange = (row) => {
  ElMessage.success(`状态更新成功`)
}

const saveBanner = () => {
  ElMessage.success(bannerDialogType.value === 'add' ? '添加成功' : '更新成功')
  bannerDialogVisible.value = false
}

// 友情链接相关方法
const handleAddLink = () => {
  linkDialogType.value = 'add'
  linkDialogVisible.value = true
  Object.keys(linkForm).forEach(key => {
    linkForm[key] = key === 'sort' ? 1 : key === 'status' ? 'active' : ''
  })
}

const handleEditLink = (row) => {
  linkDialogType.value = 'edit'
  linkDialogVisible.value = true
  Object.keys(linkForm).forEach(key => {
    linkForm[key] = row[key]
  })
}

const handleDeleteLink = (row) => {
  ElMessage.success('删除成功')
}

const handleLinkStatusChange = (row) => {
  ElMessage.success(`状态更新成功`)
}

const saveLink = () => {
  ElMessage.success(linkDialogType.value === 'add' ? '添加成功' : '更新成功')
  linkDialogVisible.value = false
}
</script>

<style scoped lang="scss">
.website-management {
  .logo-uploader,
  .banner-uploader {
    .logo,
    .banner {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: border-color 0.3s;

      &:hover {
        border-color: #409EFF;
      }
    }
  }

  .banner-preview {
    width: 200px;
    height: 100px;
    object-fit: cover;
  }

  .link-logo {
    width: 40px;
    height: 40px;
  }

  .banner-header,
  .links-header {
    margin-bottom: 20px;
  }
}
</style> 